package com.loginlib.login;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.loginlib.R;
import com.loginlib.di.component.DaggerSplashActivityComponent;
import com.loginlib.di.component.SplashActivityComponent;
import com.loginlib.di.module.SplashModule;
import com.loginlib.login.dialog.InitDataDialog;
import com.loginlib.login.dialog.SetDialog;
import com.wanhe.base.BaseActivity;
import com.wanhe.base.BaseApplication;
import com.wanhe.base.util.SPUtils;
import com.wanhe.base.util.SystemUtil;
import com.wanhe.base.util.ViewUtil;


public class SplashActivity extends BaseActivity {
    Button btnset;
    View vClickView;
    TextView versionCode;
    private SetDialog setDialog;
    private long mLastClickBackButtonTime;
    private boolean isBackClicked;
    SplashActivityComponent splashActivityComponent;
//    @Inject
//    WebSocketTool webSocketTool;
//
//    @Override
//    protected void initWindow() {
//        SystemUtil.fullScreen(this);
//        super.initWindow();
//    }

    @Override
    protected void initComponent(Bundle savedInstanceState) {
//        AppContext.getInstance().addTempActivity(this);
        setContentView(R.layout.activity_splash);
        btnset = (Button) findViewById(R.id.btnset);
        vClickView =  findViewById(R.id.v_click_view);
        versionCode = (TextView) findViewById(R.id.version_code);
        versionCode.setText("versionCode:"+getVersionCode(this));
        initEvent();
        initData();
        splashActivityComponent = DaggerSplashActivityComponent.builder().splashModule(new SplashModule(this)).build();
        splashActivityComponent.inject(this);

        if(TextUtils.isEmpty(SPUtils.getIp())){
            setDialog = new SetDialog(SplashActivity.this);
            setDialog.show();
        }

    }


    /**
     * get App versionCode
     * @param context
     * @return
     */
    public String getVersionCode(Context context){
        PackageManager packageManager=context.getPackageManager();
        PackageInfo packageInfo;
        String versionCode="";
        try {
            packageInfo=packageManager.getPackageInfo(context.getPackageName(),0);
            versionCode=packageInfo.versionCode+"";
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }


    protected void initEvent() {
        setListener();
    }

    protected void initData() {
        /**
         * 1，获取是否有缓存数据
         * 2，如果有，访问版本对比接口， 如果没有直接访问接口获取数据
         * 3，版本对比不同，访问接口缓存数据到文本文件
         * 4，版本相同，直接访问缓存文本文件加载数据
         */

        /**
         * 显示加载数据框，开始流程，流程走完，关闭加载数据框
         */

//        CatelogPresenter presenter = new CatelogPresenter();
//        presenter.getCatalog();

    }


    private void setListener() {
        btnset.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                //TODO: 2016/5/25 长按出现IP配置弹窗
                if (setDialog == null) {
                    setDialog = new SetDialog(SplashActivity.this);
                    setDialog.show();
                } else {
                    setDialog.show();
                }
                return false;
            }
        });

        vClickView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO: 2016/5/25 进入到主页面
//                new startIntent(SplashActivity.this, LogginActivity.class); // 跳转到应用应的登入页
//                new startIntent(SplashActivity.this, MainActivity.class);
//                Intent intent = new Intent(SplashActivity.this, LogginActivity.class);
//                startActivity(intent);
//                finish();
                    if(TextUtils.isEmpty(SPUtils.getIp())){
                        ViewUtil.ShortToast(BaseApplication.context,"请设置IP地址！！！");
                        if (setDialog == null) {
                            setDialog = new SetDialog(SplashActivity.this);
                            setDialog.show();
                        } else {
                            setDialog.show();
                        }
                    }else{
                        InitDataDialog dataDialog = new InitDataDialog(SplashActivity.this);
                        dataDialog.show();
                    }


            }
        });
    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            goback();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void goback() {
        long currentTime = System.currentTimeMillis();
        long detal = currentTime - mLastClickBackButtonTime;
        if (isBackClicked && detal < 1300) {
            SystemUtil.exit(this, new SystemUtil.OnAppExit() {
                @Override
                public void exit() {
//                    AppContext.getInstance().onTerminate();
                }
            });
        } else {
            isBackClicked = true;
            mLastClickBackButtonTime = currentTime;
            Toast.makeText(BaseApplication.context, getResources().getString(R.string.home_tips_exit),Toast.LENGTH_SHORT).show();
        }
    }


}
